<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>社区管理</span>
        <el-input placeholder="请输入内容" v-model="inputName" style="width: 300px;" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="search(true)"></el-button>
        </el-input>
      </div>

      <div style="min-width: 1200px;">
        <el-table  stripe :data="tableData.slice((currentPage4 - 1) * pageSize, currentPage4 * pageSize)" style="width: 100%">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column prop="nickname" label="昵称" align="center" />
          <el-table-column label="用户头像" align="center">
            <template slot-scope="scope">
              <img :src="scope.row.avatar" alt="" srcset="">
            </template>
          </el-table-column>
          <el-table-column prop="post" label="领域" align="center" />
          <el-table-column prop="post_age" label="工龄" align="center" />
          <el-table-column prop="title" label="标题" align="center" />
          <el-table-column prop="content" label="文章内容" align="center" width="300px" />
          <el-table-column label="审核状态" align="center">
            <template slot-scope="scope">
              <el-button type="success" size="min" v-if="scope.row.state === 1">审核通过</el-button>
              <el-button type="warning" size="min" v-else @click="updateStateFn(scope.row.id)">未通过</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="min" icon="el-icon-delete" @click="removeFn(scope.row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="currentPage4" :page-sizes="[3, 5, 7, 10]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getCommunityAPI,updateStateAPI,delCommunityAPI } from '@/api/community'
export default {
  data() {
    return {
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小鼠',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小牛',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小龙',
        address: '上海市普陀区金沙江路 1516 弄'
      },
      {
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小鼠',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小牛',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小龙',
        address: '上海市普陀区金沙江路 1516 弄'
      },
      {
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小鼠',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小牛',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小龙',
        address: '上海市普陀区金沙江路 1516 弄'
      },
      {
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小鼠',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小牛',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小龙',
        address: '上海市普陀区金沙江路 1516 弄'
      },
      {
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小鼠',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小牛',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小龙',
        address: '上海市普陀区金沙江路 1516 弄'
      },

      ],
      inputName: '',
      currentPage4: 1,
      pageSize: 3,
      total: 1
    }
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = 1
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage4 = val
    },
    search(bool) {
      if (bool) {
        let newListData = []
        if (this.inputName) {
          this.tableData.filter((item) => {
            if (item.nickname.indexOf(this.inputName) !== -1) {
              newListData.push(item)
            }
          });
        }
        this.tableData = newListData;
      } else {
        window.location.reload();
      }
    },
    async initgetCommunityFn() {
      const res = await getCommunityAPI()
      if (res.status !== 200) {
        this.$message.error('获取数据失败!')
      } else {
        this.$message.success('获取数据成功!')
        this.tableData = res.data.data
        this.total = res.data.total
      }
    },
    // 更改审核状态
    async updateStateFn(id){
      const res = await updateStateAPI(id)
      if(res.status !== 200){
        console.log('审核失败!');
      }else{
        console.log('审核成功');
        this.initgetCommunityFn()
      }
    },
    // 删除帖子
    async removeFn(id){
      const res = await delCommunityAPI(id)
      if(res.status !==200){
        this.$message.error('删除帖子失败!')
      }else{
        this.$message.success('删除帖子成功!')
        this.initgetCommunityFn()
      }
    }
  },
  watch: {
    inputName(newValue) {
      if (newValue) {
        this.search(true)
      } else {
        this.search(false)
      }
    }
  },
  created() {
    this.initgetCommunityFn()
  }
}
</script>

<style scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

img {
  width: 90px;
  height: 90px;
  border-radius: 90px;
}

.pagination {
  display: flex;
  justify-content: center;
  margin: 10px;
}
</style>
